/*
 * @(#)ExpectHttpMethodInterceptor.java $version 2011. 10. 14
 * 
 * Copyright 2011 NHN Corp. All rights Reserved.
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.hangame.mbl.interceptor.expecthttpmethod;

import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.nhncorp.hancore.action.AlertForward;
import com.nhncorp.lucy.common.message.MessageUtil;
import com.opensymphony.webwork.WebWorkStatics;
import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.xwork.interceptor.Interceptor;

// TODO: Auto-generated Javadoc
/**
 * The Class ExpectHttpMethodInterceptor.
 *
 * @author NHN
 */
@SuppressWarnings("serial")
public class ExpectHttpMethodInterceptor implements Interceptor, WebWorkStatics  {
	
	/** The log. */
	private static Log log = LogFactory.getLog(ExpectHttpMethodInterceptor.class);

	/**
	 * Destroy.
	 * 
	 * @see com.opensymphony.xwork.interceptor.Interceptor#destroy()
	 */
	public void destroy() {
	}

	/**
	 * Inits the.
	 * 
	 * @see com.opensymphony.xwork.interceptor.Interceptor#init()
	 */
	public void init() {
	}

	/**
	 * Intercept.
	 * 
	 * @param invocation ActionInvocation
	 * @return invocation.invoke()
	 * @throws Exception Exception
	 * @see com.opensymphony.xwork.interceptor.Interceptor#intercept(com.opensymphony.xwork.ActionInvocation)
	 */
	public String intercept(ActionInvocation invocation) throws Exception {
		ExpectHttpMethod expectHttpMethod = getAnnotation(invocation);
		
		if (expectHttpMethod != null) {
			HttpServletRequest request = (HttpServletRequest) invocation.getInvocationContext().get(HTTP_REQUEST);			
			String actualMethod = request.getMethod().toUpperCase();
			String expectedMethod = expectHttpMethod.method().toUpperCase();
			
			if (!expectedMethod.equals(ExpectHttpMethod.ALL) && !expectedMethod.equals(actualMethod)) {
				String msg = MessageUtil.getMessage("error.notAllowedHttpMethod", actualMethod);
				String nextURL = request.getRequestURL().toString();
				log.debug("msg : " + msg);
				log.debug("nextURL" + nextURL);
				return AlertForward.alertAndGoPageCustomMessage(msg, nextURL); 
			}
		}
		
		return invocation.invoke();
	}
	
	/**
	 * Gets the annotation.
	 * 
	 * @param invocation ActionInvocation
	 * @return ExpectHttpMethod
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private ExpectHttpMethod getAnnotation(ActionInvocation invocation) {
		Class actionClazz = invocation.getAction().getClass();
		String methodName = invocation.getProxy().getMethod();
		ExpectHttpMethod expectHttpMethod = null;
		
		try {
			Method method = actionClazz.getMethod(methodName, new Class[0]);
			expectHttpMethod = method.getAnnotation(ExpectHttpMethod.class);
		} catch (SecurityException e) {
			log.debug(e);
		} catch (NoSuchMethodException e) {
			log.debug(e);
		}
		
		return expectHttpMethod;
	}

}
